// 僵尸类
let zombiesType = [{type:0,num:30,name:'one'}]
class Zombies {
    constructor(obj) {
        let s = {
            x:obj.x,
            y:obj.y,
            id:obj.id,
            name:obj.name,
            type:obj.type, //僵尸种类
            live:10, //生命值
        }
        Object.assign(this,s)
    }
    draw(){
        // 僵尸帧动画
        let i = 0
        let zombie = {}
        let img = new Image()
        img.src = './images/zombies/idle/'+this.name
        img.className = 'zombie'
        app.appendChild(img)
        let zom = document.querySelector('.zombie')
        let src = zom.src
        zom.style.position = 'absolute'
        zom.style.left = this.x + 'px'
        zom.style.top = this.y + 'px'
        zom.style.height = '100px'
        zom.className =  'zoms'
        let speed = 0
        zombiesType.forEach(item=>{
            if(item.type === this.type){
                zombie = item
                return
            }
        })
        // 僵尸帧动画
        this['zom'+this.id] = setInterval(()=>{
            i++
            i<10&&(i='0'+i)
            zom.src = src.replace(/00/g,i)
            i>=zombie.num&&(i=0)
            let bullets = document.querySelectorAll('.bullets')
            // 僵尸向前行走
            speed += 2
            // 检测边界
            if(parseFloat(zom.style.left)<= -152){
                clearInterval(this['zom'+this.id]) //僵尸暂停
                timers.forEach(item=>{
                    clearInterval(item)
                })
                // zom.remove()
                // 弹出游戏结束
                let gameOver = new Image()
                gameOver.src = './images/zombieWon.png'
                gameOver.style.transform = 'translate(-50%,-50%)'
                gameOver.style.position = 'absolute'
                gameOver.style.top = '50%'
                gameOver.style.left = '50%'
                app.appendChild(gameOver)
            }
            // 检测障碍物，子弹
            bullets.forEach(item=>{
                if(parseFloat(zom.style.left) <= parseFloat(item.style.left)&&parseFloat(zom.style.top)<= parseFloat(item.style.top)&&parseFloat(zom.style.top)>= parseFloat(item.style.top)-80){
                    // 生命值减少
                    if(this.live<=0){
                        clearInterval(this['zom'+this.id]) //僵尸暂停
                        timers.pop()
                        // 僵尸死亡帧动画
                        zom.remove()
                        i = 0
                        let die = new Image()
                        die.src = './images/zombies/die/die_01.png'
                        die.style.left = zom.style.left
                        die.style.top = zom.style.top
                        die.style.height = '100px'
                        die.style.position = 'absolute'
                        let diesrc = die.src
                        app.appendChild(die)
                        dieTimer.push(this['zomDie'+this.id])
                        this['zomDie'+this.id] = setInterval(()=>{
                            i++
                            i<10&&(i='0'+i)
                            die.src = diesrc.replace(/01/g,i)
                            if(i>=9){
                                die.remove()
                                clearInterval(this['zomDie'+this.id])
                            }
                        },300)
                    }else{
                        zom.animate([{opacity:0.6},{opacity:1}],keyframesOptions)
                        // 判断子弹类型，减掉对应生命值
                        if(item.classList.contains('bullet2')&&item.classList.contains('bullet_fire')){
                            this.live = this.live - 2 - 1.5
                        }
                        else if(item.classList.contains('bullet1')&&item.classList.contains('bullet_fire')){
                            this.live = this.live - 1.5 - 1.5
                        }
                        else if(item.classList.contains('bullet2')){
                            this.live = this.live - 2
                        }
                        else{
                            this.live -= 1.5
                        }
                    }
                }
            })
            zom.style.left = this.x - speed + 'px'
        },80)
        // 收集计时器
        timers.push(this['zom'+this.id])
    }
}

